home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1996 February / EnigmA AMIGA RUN 04 (1996)(G.R. Edizioni)(IT)[!][issue 1996-02][Skylink CD III].iso / earcd / comm2 / termsorc.lha / Extras / Source / gtlayout-source.lha / LTP_GlyphSetup.c < prev    next >
C/C++ Source or Header  |  1995-09-24  |  3KB  |  185 lines

  1. /*  GadTools layout toolkit
  2. **
  3. **  Copyright © 1993-1995 by Olaf `Olsen' Barthel
  4. **  Freely distributable.
  5. */
  6.  
  7. #include "gtlayout_global.h"
  8.  
  9. VOID __regargs
  10. LTP_GetDefaultFont(struct TTextAttr *TextAttr)
  11. {
  12.     struct TextFont *Font = GfxBase -> DefaultFont;
  13.  
  14.     TextAttr -> tta_Name    = Font -> tf_Message . mn_Node . ln_Name;
  15.     TextAttr -> tta_YSize    = Font -> tf_YSize;
  16.     TextAttr -> tta_Style    = Font -> tf_Style;
  17.     TextAttr -> tta_Flags    = Font -> tf_Flags & ~FPF_REMOVED;
  18.  
  19.     if(Font -> tf_Style & FSF_TAGGED)
  20.         TextAttr -> tta_Tags = ((struct TextFontExtension *)Font -> tf_Extension) -> tfe_Tags;
  21.     else
  22.         TextAttr -> tta_Tags = NULL;
  23. }
  24.  
  25.  
  26. /*****************************************************************************/
  27.  
  28.  
  29. struct TextFont * __regargs
  30. LTP_OpenFont(struct TextAttr *TextAttr)
  31. {
  32.     struct TextFont *Font;
  33.  
  34.         // Let's see if this one is special
  35.  
  36.     if(TextAttr == (struct TextAttr *)~0)
  37.     {
  38.         struct TTextAttr LocalTextAttr;
  39.  
  40.         memset(&LocalTextAttr,0,sizeof(LocalTextAttr));
  41.  
  42.         Forbid();
  43.  
  44.             // Borrow the system default font data
  45.  
  46.         LTP_GetDefaultFont(&LocalTextAttr);
  47.  
  48.             // Let's hope the font will open...
  49.  
  50.         Font = OpenFont((struct TextAttr *)&LocalTextAttr);
  51.  
  52.         Permit();
  53.     }
  54.     else
  55.     {
  56.         if(!(Font = OpenFont(TextAttr)))
  57.         {
  58.             if(SysBase -> ThisTask -> tc_Node . ln_Type != NT_TASK)
  59.             {
  60.                 struct Library *DiskfontBase;
  61.  
  62.                 if(DiskfontBase = OpenLibrary("diskfont.library",0))
  63.                 {
  64.                     Font = OpenDiskFont(TextAttr);
  65.  
  66.                     CloseLibrary(DiskfontBase);
  67.                 }
  68.             }
  69.         }
  70.     }
  71.  
  72.     return(Font);
  73. }
  74.  
  75.  
  76. /*****************************************************************************/
  77.  
  78.  
  79. BOOLEAN __regargs
  80. LTP_GlyphSetup(struct LayoutHandle *Handle,struct TextAttr *TextAttr)
  81. {
  82.     struct TextFont *Font;
  83.     struct RastPort *RPort;
  84.  
  85.     RPort = &Handle -> RPort;
  86.  
  87.     if(TextAttr)
  88.     {
  89.         Handle -> TextAttr = TextAttr;
  90.  
  91.         if(Font = LTP_OpenFont(Handle -> TextAttr))
  92.         {
  93.             if(Handle -> CloseFont)
  94.                 CloseFont(RPort -> Font);
  95.             else
  96.                 Handle -> CloseFont = TRUE;
  97.         }
  98.     }
  99.     else
  100.     {
  101.         Handle -> TextAttr = Handle -> Screen -> Font;
  102.  
  103.         Font = Handle -> DrawInfo -> dri_Font;
  104.  
  105.         if(Handle -> CloseFont)
  106.         {
  107.             CloseFont(RPort -> Font);
  108.  
  109.             Handle -> CloseFont = FALSE;
  110.         }
  111.     }
  112.  
  113.     if(Font)
  114.     {
  115.         LONG    Count = 0;
  116.         LONG    Total = 0;
  117.         UBYTE    Glyph;
  118.         UWORD    i;
  119.         ULONG    OldStyle;
  120.  
  121.         LTP_SetFont(Handle,Font);
  122.  
  123.         OldStyle = SetSoftStyle(RPort,FSF_BOLD,AskSoftStyle(RPort));
  124.  
  125.             // Ok, first cover the standard ASCII character set
  126.  
  127.         for(i = 32 ; i < 127 ; i++,Count++)
  128.         {
  129.             Glyph = i;
  130.  
  131.             Total += TextLength(RPort,&Glyph,1);
  132.         }
  133.  
  134.             // Now for the ISO part...
  135.  
  136.         for(i = 160 ; i < 256 ; i++,Count++)
  137.         {
  138.             Glyph = i;
  139.  
  140.             Total += TextLength(RPort,&Glyph,1);
  141.         }
  142.  
  143.         Handle -> GlyphWidth = Total / Count;
  144.  
  145.             // Now for the numeric characters
  146.  
  147.         for(Total = 0, i = '0' ; i <= '9' ; i++)
  148.         {
  149.             Glyph = i;
  150.  
  151.             Total += TextLength(RPort,&Glyph,1);
  152.         }
  153.  
  154.             // Actually, the numeric character should be
  155.             // just as wide as the space character. Let's
  156.             // hope for the best.
  157.  
  158.         Count = TextLength(RPort," ",1);
  159.  
  160.         Total /= 10;
  161.  
  162.         if(Total < Count)
  163.             Total = Count;
  164.  
  165.         if(Total > Handle -> GlyphWidth)
  166.             Handle -> GlyphWidth = Total;
  167.  
  168.         if(Handle -> GlyphWidth <= 8)
  169.             Handle -> InterWidth = 4;
  170.         else
  171.             Handle -> InterWidth = Handle -> GlyphWidth / 2;
  172.  
  173.         if(RPort -> TxHeight <= 8)
  174.             Handle -> InterHeight = 2;
  175.         else
  176.             Handle -> InterHeight = RPort -> TxHeight / 4;
  177.  
  178.         SetSoftStyle(RPort,OldStyle,~0);
  179.  
  180.         return(TRUE);
  181.     }
  182.     else
  183.         return(FALSE);
  184. }
  185.